package formularios;

import modelo.Usuario;
import consistencia.ValidaCampoNumerico;
import java.util.logging.Level;
import java.util.logging.Logger;
import dao.Resultado;
import javax.swing.JOptionPane;
import static dao.BancoDeDados.*;

public class frmCadastroUsuario extends javax.swing.JInternalFrame {

    int matricula = 0;

    /** Creates new form CadastroUsuario */
    public frmCadastroUsuario() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel13 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabelMatricula = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtMatricula = new ValidaCampoNumerico();
        txtSenha = new javax.swing.JTextField();
        txtCpf = new javax.swing.JTextField();
        txtRg = new javax.swing.JTextField();
        txtTelefoneResidencial = new javax.swing.JTextField();
        txtTelefoneCelular = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        txtDataNascimento = new javax.swing.JTextField();
        txtEndereco = new javax.swing.JTextField();
        txtCidade = new javax.swing.JTextField();
        cboTipo = new javax.swing.JComboBox();
        jLabel14 = new javax.swing.JLabel();
        txtEstado = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        btnNovo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        cboSexo = new javax.swing.JComboBox();
        jLabel16 = new javax.swing.JLabel();

        jLabel13.setText("jLabel13");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 36));
        jLabel5.setText("Cadastro de Usuários");

        jLabelMatricula.setText("Matricula:");

        jLabel2.setText("Senha:");

        jLabel3.setText("Tipo de Usuário:");

        jLabel4.setText("Cpf:");

        jLabel6.setText("Rg:");

        jLabel7.setText("Tel. Residencial:");

        jLabel8.setText("Tel. Celular:");

        jLabel9.setText("Email:");

        jLabel10.setText("Endereço:");

        jLabel11.setText("Cidade:");

        jLabel12.setText("Data Nascimento:");

        txtMatricula.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtMatriculaFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtMatriculaFocusLost(evt);
            }
        });

        cboTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vendedor", "Gerente", "Administrador" }));

        jLabel14.setText("Estado:");

        jLabel15.setText("Nome:");

        btnNovo.setText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnEditar.setText("Editar Usuario");
        btnEditar.setEnabled(false);
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnSalvar.setText("Salvar");
        btnSalvar.setEnabled(false);
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnExcluir.setText("Excluir");
        btnExcluir.setEnabled(false);
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        cboSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Selecione>", "Masculino", "Feminino" }));
        cboSexo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboSexoActionPerformed(evt);
            }
        });

        jLabel16.setText("Sexo:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(btnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(102, 102, 102)
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(92, 92, 92))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel10)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel14)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel7))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addComponent(jLabelMatricula)
                            .addComponent(jLabel15)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel12))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtEstado, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtCidade, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(txtTelefoneResidencial, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jLabel8)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txtTelefoneCelular))
                                .addComponent(txtEndereco)
                                .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtNome)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(txtCpf, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtSenha, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                                            .addComponent(txtDataNascimento, javax.swing.GroupLayout.Alignment.LEADING))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addComponent(jLabel3)
                                                    .addComponent(jLabel6))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(txtRg)
                                                    .addComponent(cboTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(jLabel16)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(cboSexo, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)))))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel15)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(cboTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtDataNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cboSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel16)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTelefoneCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(txtTelefoneResidencial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14))
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNovo)
                    .addComponent(btnEditar)
                    .addComponent(btnSalvar)
                    .addComponent(btnExcluir)
                    .addComponent(btnCancelar))
                .addContainerGap(47, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        limparCampos();
        habilitarCampos();
        btnSalvar.setEnabled(true);
        txtMatricula.setVisible(false);
        jLabelMatricula.setVisible(false);
        txtNome.requestFocusInWindow();
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        habilitarCampos();
        btnSalvar.setEnabled(true);
        btnExcluir.setEnabled(false);
        btnEditar.setEnabled(false);
}//GEN-LAST:event_btnEditarActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed

        if (matricula == 0) {
            try {
                String nome = txtNome.getText();
                String senha = txtSenha.getText();
                String tipo_usuario;
                String cpf = txtCpf.getText();
                String rg = txtRg.getText();
                String data_nascimento = txtDataNascimento.getText();
                String sexo;
                String telefone1 = txtTelefoneResidencial.getText();
                String telefone2 = txtTelefoneCelular.getText();
                String email = txtEmail.getText();
                String endereco = txtEndereco.getText();
                String cidade = txtCidade.getText();
                String estado = txtEstado.getText();
                if (cboTipo.getSelectedIndex() == 0) {
                    //define tipo_usuario de usuario
                    tipo_usuario = "1"; //Vendedor
                } else if (cboTipo.getSelectedIndex() == 1) {
                    tipo_usuario = "2"; // Gerente
                } else {
                    tipo_usuario = "3"; //Administrador
                }
                if (cboSexo.getSelectedIndex() == 1) {
                    //define sexo
                    sexo = "M";
                } else if (cboSexo.getSelectedIndex() == 2) {
                    sexo = "F";
                } else {
                    JOptionPane.showMessageDialog(this, "Selecione um sexo");
                    cboSexo.requestFocusInWindow();
                    return;
                }
                Object[] usuario = new Object[13];
                usuario[0] = nome;
                usuario[1] = senha;
                usuario[2] = tipo_usuario;
                usuario[3] = cpf;
                usuario[4] = rg;
                usuario[5] = data_nascimento;
                usuario[6] = sexo;
                usuario[7] = telefone1;
                usuario[8] = telefone2;
                usuario[9] = email;
                usuario[10] = endereco;
                usuario[11] = cidade;
                usuario[12] = estado;
                executaOperacao("insert into usuario (nome, senha, tipo_usuario, cpf, rg, data_nascimento, sexo, telefone_resid, telefone_cel, email, endereco, cidade, estado) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", usuario);
                JOptionPane.showMessageDialog(this, "Usuario cadastrado com sucesso.");
                Object[] cpf_usuario = new Object[1];
                cpf_usuario[0] = cpf;
                Resultado rs_cliente = executaConsulta("Select * from usuario where cpf = ?", cpf_usuario);
                if (rs_cliente.qtdRegistros() != 0) {
                    matricula = ((Integer) rs_cliente.valorCampo(0, "matricula"));
                } else {
                    matricula = 0;
                }
                txtMatricula.setText(String.valueOf(matricula));
                desabilitarCampos();
            } catch (Exception ex) {
                Logger.getLogger(frmCadastroUsuario.class.getName()).log(Level.SEVERE, null, ex);
            }


        } else { // matricula diferente de zero = editar

            try {
                // matricula diferente de zero = editar
                String nome = txtNome.getText();
                String senha = txtSenha.getText();
                String tipo;
                String cpf = txtCpf.getText();
                String rg = txtRg.getText();
                String data_nascimento = txtDataNascimento.getText();
                String sexo;
                String telefone_resid = txtTelefoneResidencial.getText();
                String telefone_cel = txtTelefoneCelular.getText();
                String email = txtEmail.getText();
                String endereco = txtEndereco.getText();
                String cidade = txtCidade.getText();
                String estado = txtEstado.getText();
                if (cboTipo.getSelectedIndex() == 0) {
                    //define tipo_usuario de usuario
                    tipo = "1"; //Vendedor
                } else if (cboTipo.getSelectedIndex() == 1) {
                    tipo = "2"; //Administrador
                } else {
                    tipo = "3"; //Administrador
                }
                if (cboSexo.getSelectedIndex() == 1) {
                    //define sexo
                    sexo = "M";
                } else if (cboSexo.getSelectedIndex() == 2) {
                    sexo = "F";
                } else {
                    JOptionPane.showMessageDialog(this, "Selecione um sexo");
                    cboSexo.requestFocusInWindow();
                    return;
                }
                desabilitarCampos();
                Object[] usuario = new Object[14];
                usuario[0] = nome;
                usuario[1] = senha;
                usuario[2] = tipo;
                usuario[3] = cpf;
                usuario[4] = rg;
                usuario[5] = data_nascimento;
                usuario[6] = sexo;
                usuario[7] = telefone_resid;
                usuario[8] = telefone_cel;
                usuario[9] = email;
                usuario[10] = endereco;
                usuario[11] = cidade;
                usuario[12] = estado;
                usuario[13] = matricula;
                executaOperacao("update usuario set nome = ?, senha = ?, tipo_usuario = ?, cpf = ?, rg = ?, data_nascimento = ?, sexo = ?, telefone_resid = ?, telefone_cel = ?, email = ?, endereco = ?, cidade = ?, estado = ? where matricula = ?", usuario);
                JOptionPane.showMessageDialog(this, "Usuario alterado com sucesso.");
            } catch (Exception ex) {
                Logger.getLogger(frmCadastroUsuario.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        btnSalvar.setEnabled(false);
        txtMatricula.setVisible(true);
        jLabelMatricula.setVisible(true);
        btnEditar.setEnabled(true);
        btnExcluir.setEnabled(true);

}//GEN-LAST:event_btnSalvarActionPerformed
    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        try {
            if (matricula == 0) {
                JOptionPane.showMessageDialog(this, "Selecione um usuario para excluir!");
                return;
            }
            Object[] mat_usuario = new Object[1];
            mat_usuario[0] = matricula;
            executaOperacao("delete from usuario where matricula = ?", mat_usuario);
            JOptionPane.showMessageDialog(this, "Usuario excluído com sucesso!");
            txtMatricula.requestFocusInWindow();
        } catch (Exception ex) {
            Logger.getLogger(frmCadastroUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_btnExcluirActionPerformed
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        frmCadastroUsuario.this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed
    private void cboSexoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboSexoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_cboSexoActionPerformed

    private void txtMatriculaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtMatriculaFocusLost

        matricula = Integer.parseInt("0" + txtMatricula.getText());

        if (matricula == 0) {

            txtMatricula.requestFocusInWindow();
            return;

        } else {

            Object[] mat_usuario = new Object[1];
            mat_usuario[0] = matricula;

            Resultado rs_usuario = executaConsulta("Select * from usuario where matricula = ?", mat_usuario);


            Usuario usuario = new Usuario();

            if (rs_usuario.qtdRegistros() != 0){
                usuario.setMatricula((Integer) rs_usuario.valorCampo(0, "matricula"));
                usuario.setNome((String) rs_usuario.valorCampo(0, "nome"));
                usuario.setSenha((String) rs_usuario.valorCampo(0, "senha"));
                usuario.setTipoUsuario((Integer) rs_usuario.valorCampo(0, "tipo_usuario"));
                usuario.setCpf((String) rs_usuario.valorCampo(0, "cpf"));
                usuario.setRg((String) rs_usuario.valorCampo(0, "rg"));
                usuario.setDataNascimento((String) rs_usuario.valorCampo(0, "data_nascimento"));
                usuario.setSexo((String) rs_usuario.valorCampo(0, "sexo"));
                usuario.setTelefoneResidencial((String) rs_usuario.valorCampo(0, "telefone_resid"));
                usuario.setTelefoneCelular((String) rs_usuario.valorCampo(0, "telefone_cel"));
                usuario.setEmail((String) rs_usuario.valorCampo(0, "email"));
                usuario.setEndereco((String) rs_usuario.valorCampo(0, "endereco"));
                usuario.setCidade((String) rs_usuario.valorCampo(0, "cidade"));
                usuario.setEstado((String) rs_usuario.valorCampo(0, "estado"));

                txtNome.setText(usuario.getNome());
                txtSenha.setText(usuario.getSenha());
                if (usuario.getTipoUsuario().equals(1)) { //1 Vendedor
                    cboTipo.setSelectedIndex(0); // indice 0 vendedor
                } else if (usuario.getTipoUsuario().equals(2)) { //2 Gerente
                    cboTipo.setSelectedIndex(1);// indice 1 gerente
                } else { //3 Administrador
                    cboTipo.setSelectedIndex(1);// indice 2 Administrador
                }
                txtCpf.setText(usuario.getCpf());
                txtRg.setText(usuario.getRg());
                txtDataNascimento.setText(usuario.getDataNascimento());
                if (usuario.getSexo().equals("M")) {
                    cboSexo.setSelectedIndex(1);
                } else {
                    cboSexo.setSelectedIndex(2);
                }
                txtTelefoneResidencial.setText(usuario.getTelefoneResidencial());
                txtTelefoneCelular.setText(usuario.getTelefoneCelular());
                txtEmail.setText(usuario.getEmail());
                txtEndereco.setText(usuario.getEndereco());
                txtCidade.setText(usuario.getCidade());
                txtEstado.setText(usuario.getEstado());


                btnEditar.setEnabled(true);
                btnExcluir.setEnabled(true);
                desabilitarCampos();

            } else {
                matricula = 0;
                JOptionPane.showMessageDialog(this, "Não existe usuario cadastrado com essa matricula!");
                txtMatricula.requestFocusInWindow();
            }
        }
    }//GEN-LAST:event_txtMatriculaFocusLost

    private void txtMatriculaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtMatriculaFocusGained
        limparCampos();
    }//GEN-LAST:event_txtMatriculaFocusGained
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox cboSexo;
    private javax.swing.JComboBox cboTipo;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelMatricula;
    private javax.swing.JTextField txtCidade;
    private javax.swing.JTextField txtCpf;
    private javax.swing.JTextField txtDataNascimento;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtEndereco;
    private javax.swing.JTextField txtEstado;
    private javax.swing.JTextField txtMatricula;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtRg;
    private javax.swing.JTextField txtSenha;
    private javax.swing.JTextField txtTelefoneCelular;
    private javax.swing.JTextField txtTelefoneResidencial;
    // End of variables declaration//GEN-END:variables

    private void habilitarCampos() {
        txtNome.setEditable(true);
        txtSenha.setEditable(true);
        cboTipo.setEnabled(true);
        txtCpf.setEditable(true);
        txtRg.setEditable(true);
        txtDataNascimento.setEditable(true);
        cboSexo.setEnabled(true);
        txtTelefoneResidencial.setEditable(true);
        txtTelefoneCelular.setEditable(true);
        txtEmail.setEditable(true);
        txtEndereco.setEditable(true);
        txtCidade.setEditable(true);
        txtEstado.setEditable(true);
    }

    private void limparCampos() {
        txtMatricula.setText("");
        txtNome.setText("");
        txtSenha.setText("");
        cboTipo.setSelectedIndex(0); // indice 0 vendedor
        txtCpf.setText("");
        txtRg.setText("");
        txtDataNascimento.setText("");
        cboSexo.setSelectedIndex(0);
        txtTelefoneResidencial.setText("");
        txtTelefoneCelular.setText("");
        txtEmail.setText("");
        txtEndereco.setText("");
        txtCidade.setText("");
        txtEstado.setText("");
        matricula = 0;
        btnEditar.setEnabled(false);
        btnExcluir.setEnabled(false);

    }

    private void desabilitarCampos() {
        txtNome.setEditable(false);
        txtSenha.setEditable(false);
        cboTipo.setEnabled(false); // indice 0 vendedor
        txtCpf.setEditable(false);
        txtRg.setEditable(false);
        txtDataNascimento.setEditable(false);
        cboSexo.setEnabled(false);
        txtTelefoneResidencial.setEditable(false);
        txtTelefoneCelular.setEditable(false);
        txtEmail.setEditable(false);
        txtEndereco.setEditable(false);
        txtCidade.setEditable(false);
        txtEstado.setEditable(false);
    }
}
